# Launching the engine

## The basics

When using the nkGraphics component within the Nilkins Engine, the first step is to initialize it through the API. This is required to prepare the component, allocate all basic resources it needs, and launch the back end requested.

Basic initialization go through including :

#include <NilkinsGraphics/System.h>

And then using the class through :

if (!nkGraphics::System::getInstance()->initialize()) return -1 ;

The System is a singleton that can be accessed through the getInstance method. Calling the initialize method on it will trigger all above-mentioned preparation steps. Checking on the result can let you know if the component could initialize itself correctly or not. If it is not the case, the component cannot be used.

Once the engine is initialized, what is needed next, is to create a RenderContext. This represents a visible "surface" in which the component will render to. What contexts can do will be covered in a later tutorial. For now, we will just create one, requesting it to be linked to a window. First, include :

#include <NilkinsGraphics/RenderContexts/RenderContextDescriptor.h> #include <NilkinsGraphics/RenderContexts/RenderContextManager.h>

Which allows to do :

nkGraphics::RenderContext* context = nkGraphics::RenderContextManager::getInstance()->createRenderContext(nkGraphics::RenderContextDescriptor(800, 600, false, true)) ;

In a nutshell, what we now have is a rendering context, that has been linked to a window that should pop automatically on screen. Note that under the hood, nkGraphics uses nkWinUi for the window handling. Back to the program, if you launch current code the window will close itself right away, and program will exit.

What we miss is the rendering loop. As such, the program automatically finishes and clean everything. Again, more details about the rendering loop will come in time in a dedicated tutorial, as this can be a complex challenge. For now, what we will do is simply ask the component to run by itself :

nkGraphics::System::getInstance()->run(context) ;

The run method takes a RenderContext as a parameter. This is required to know which context the rendering loop should render to.

With this code added, running the program will result in something like :

Default window once the component is running

There is not a lot to do with this window apart from closing it, for now. However, we also have a last step to think about when exiting the program : requesting a clean of the component's memory. For that, after the run loop call, add :

nkGraphics::System::getInstance()->kill() ;

This method is from the MaybeSingletonClass, and will delete the instance. The System automatically calls its shutdown method during deletion, which means all memory will be cleaned for a safe exiting of the program.

And with all of that, congratulations ! You now have the nkGraphics component set up and ready to be used.

## Using the logging capabilities

Currently the engine is quite silent about what is happening inside. This is because we did not plug any logger inside it. Let's change that so that we can witness what the component has to tell us. We will use the provided console logger for easiness. If your program doesn't use the console, you can use the file logger, rather. First, include :

#include <NilkinsLog/Loggers/ConsoleLogger.h> // Required for the unique_ptr #include <memory>

And, before calling the initialization method, add this :

std::unique_ptr<nkLog::Logger> logger = std::make_unique<nkLog::ConsoleLogger>() ; nkGraphics::LogManager::getInstance()->setReceiver(logger.get()) ;

Launching the program, you should notice a difference in the console :

The component now logs its initialization steps

Whenever a problem occurs, the component will use the logger provided to give a shout. Plugging one that is comfortable to use is important to be able to troubleshoot problems easily.

# Pushing forward

The component being ready to be used, and now giving all the information we need, it is time to move on to next tutorials, from which more and more capabilities of the engine will be demonstrated !